package com.houger.license;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class LicenseController {

    private final LicenseService licenseService;

    public LicenseController(LicenseService licenseService) {
        this.licenseService = licenseService;
    }

    @GetMapping("/fingerprint")
    public ResponseEntity<String> getFingerprint() {
        return ResponseEntity.ok(licenseService.getCurrentFingerprint());
    }

    @PostMapping("/activate")
    public ResponseEntity<String> activateLicense(@RequestParam String key) {
        return licenseService.activate(key) ?
                ResponseEntity.ok("激活成功") :
                ResponseEntity.badRequest().body("无效的激活码");
    }

    @GetMapping("/feature")
    public ResponseEntity<Long> protectedFeature() {
        return ResponseEntity.ok(licenseService.getLastDay());
    }

    @ExceptionHandler(LicenseException.class)
    public ResponseEntity<String> handleLicenseException(LicenseException e) {
        return ResponseEntity.status(403).body(e.getMessage());
    }
}